package com.uottawa.librarytest.service;

import java.awt.List;
import java.util.ArrayList;
import java.util.Random;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.uottawa.library.domain.Item;
import com.uottawa.library.domain.ItemFactory;
import com.uottawa.library.domain.LoanPeriod;
import com.uottawa.library.domain.Title;
import com.uottawa.library.domain.User;
import com.uottawa.library.service.LoanPeriodService;

public class FlyweightPatternTest {

	ArrayList<Title> titleList = null;
	@Before
	public void setUp() {
		titleList = new ArrayList<Title>();
		Title title1 = new Title(18123445, "Magic Shame");
		Title title2 = new Title(18123444, "Catcher in the Rye");
		Title title3 = new Title(18123476, "The Full Moon");
		Title title4 = new Title(18123432, "The Eagle Has Ended");
		Title title5 = new Title(18125674, "Shogun");
		Title title6 = new Title(12534625, "The Grapes of Wrath");
		Title title7 = new Title(18145767, "Das Parfum");
		Title title8 = new Title(76465783, "Nothingman"); //Well yeah, this is just a song of Pearl Jam. Not a book, obviously!
		Title title9 = new Title(18345567, "The Horse Whisperer");
		Title title10 = new Title(13459978, "The Hitchhiker's Guide to the Galaxy"); //Favorite of all time!
		
		titleList.add(title1);
		titleList.add(title2);
		titleList.add(title3);
		titleList.add(title4);
		titleList.add(title5);
		titleList.add(title6);
		titleList.add(title7);
		titleList.add(title8);
		titleList.add(title9);
		titleList.add(title10);
	}

	@After
	public void cleanUp() {
		titleList.clear();
	}
	
	@Test
	public void test1(){
		int min = 0;
		int max = 9;
		Title innerT = null;
		
		System.out.println("--- WITHOUT FLYWEIGHT PATTERN ---");
		int numberOfCreatedObjects = 0;
		long startTime = System.currentTimeMillis();
		for(int i = 0; i< 20; i++){
			Random r = new Random();
			int i1 = r.nextInt(max - min + 1) + min;
			innerT = titleList.get(i1);
			Item item = new Item(i, innerT.getIsbn());
			numberOfCreatedObjects++;
			System.out.println(item.getItemId());
		}
		long endTime = System.currentTimeMillis();
		System.out.println("Total number of created objects: " + numberOfCreatedObjects);

//		System.out.println("Total execution time: "  +  (endTime-startTime));
	}
	
	
	@Test
	public void test2(){
		int min = 0;
		int max = 9;
		Title innerT = null;
		Random r = new Random();

		System.out.println("--- WITH FLYWEIGHT PATTERN ---");
		long startTime = System.currentTimeMillis();
		for(int i = 0; i<= 20; i++){
			int i1 = r.nextInt(max - min + 1) + min;
			innerT = titleList.get(i1);
			Item item = ItemFactory.getItem(i, innerT);
			System.out.println(item.getItemId());
		}
		long endTime = System.currentTimeMillis();
		
//		System.out.println("Total execution time: "  +  (endTime-startTime));
		System.out.println("Total number of created objects: " + ItemFactory.numberOfItems);
	}
}
